/*
 * Copyright (C) 2010 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yyfax.ph.common.scan.idcard;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

class PictureCallback implements Camera.PictureCallback {

    private static final String TAG = PreviewCallback.class.getSimpleName();

    PictureCallback() {
    }

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
//        if (CameraManager.get().getCCM().getPictureFormat() == PixelFormat.JPEG) {
//        }
        saveImage(data);
//        if (this.captureListener != null) {
//            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
//            int width = bitmap.getWidth();
//            int height = bitmap.getHeight();
//            int ret = 0;
//            EXOCREngine ocrengine = new EXOCREngine();
//
//            ret = EXOCREngine.nativeRecoIDCardBitmap(bitmap, ocrengine.bResultBuf, ocrengine.bResultBuf.length);
//
//            if (ret > 0) {
//                EXIDCardResult idcard = EXIDCardResult.decode(ocrengine.bResultBuf, ret);
//                if (idcard != null) {
//                    idcard.SetViewType("Preview");
//                    Message message = Message.obtain(captureListener.getHandler(), ViewUtil.getResourseIdByName(captureListener.getAPPpackageName(), "id", "decode_succeeded"), idcard);
//                    message.sendToTarget();
//                }
//            }
//            this.captureListener = null;
//        }
        CameraManager.get().startPreview();
    }

    private void saveImage(byte[] data) { // 保存jpg到SD卡中
        BufferedOutputStream bos = null;
        Bitmap bm = null;
        try {
            // 获得图片
            bm = BitmapFactory.decodeByteArray(data, 0, data.length);
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                Log.i(TAG, "Environment.getExternalStorageDirectory()=" + Environment.getExternalStorageDirectory());
                String filePath = Environment.getExternalStorageDirectory().getPath()+"/dkynewtakepicture" + System.currentTimeMillis() + ".jpg";//照片保存路径
                File file = new File(filePath);
                if (!file.exists()) {
                    file.createNewFile();
                }
                bos = new BufferedOutputStream(new FileOutputStream(file));
                bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);//将图片压缩到流中

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bos!=null){
                    bos.flush();//输出
                    bos.close();//关闭
                }
                if (bm!=null)
                    bm.recycle();// 回收bitmap空间
                CameraManager.get().stopPreview();
                CameraManager.get().startPreview();
//                mCamera.stopPreview();// 关闭预览
//                mCamera.startPreview();// 开启预览
            } catch (IOException | NullPointerException e) {
                e.printStackTrace();
            }
        }
    }


}
